package com.mfangsoft.zhuangjialong.app.weixin.service.impl;

import java.io.File;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.app.appNewActivity.mapper.AppNewActivityMapper;
import com.mfangsoft.zhuangjialong.app.weixin.mapper.WeiXinMapper;
import com.mfangsoft.zhuangjialong.app.weixin.model.QRCodeModel;
import com.mfangsoft.zhuangjialong.app.weixin.model.TouTiaoRegister;
import com.mfangsoft.zhuangjialong.app.weixin.model.WeiXinConst;
import com.mfangsoft.zhuangjialong.app.weixin.model.WeiXinPhone;
import com.mfangsoft.zhuangjialong.app.weixin.model.WeixinReturnValue;
import com.mfangsoft.zhuangjialong.app.weixin.qrcode.QRCodeUtil;
import com.mfangsoft.zhuangjialong.app.weixin.service.IWeiXinService;
import com.mfangsoft.zhuangjialong.app.weixin.util.WeiXinUtil;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.CacheUtil;
import com.mfangsoft.zhuangjialong.common.utils.PropUtis;
import com.mfangsoft.zhuangjialong.common.utils.SendSMS;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.region.model.RegionEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.SellerPointEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.BaseUserManage;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.SellerPointEntity;

@Service
public class WeiXinServiceImpl implements IWeiXinService {

	@Autowired
	WeiXinMapper weiXinMapper;
	@Autowired
	private SellerPointEntityMapper sellerPointEntityMapper;
	@Autowired
	AppNewActivityMapper appNewActivityMapper;

	@Override
	public Map<String, Object> getWeiXinInfo(Map<String, Object> map) {
		WeixinReturnValue weixinReturnValue = WeiXinUtil.getSessionKey(WeiXinConst.APPID, WeiXinConst.APPSECRET,
				map.get("js_code").toString());
		if(weixinReturnValue!=null){
			Integer type = map.get("type") == null ? 0 : Integer.valueOf(map.get("type").toString());
			Integer userId = map.get("value") == null ? 0 : Integer.valueOf(map.get("value").toString());
			Map<String,Object> customer = weiXinMapper.selectCustomerByOpenid(weixinReturnValue.getOpenid());
			if(customer!=null && customer.get("user_id")!=null){
				Integer seller = weiXinMapper.selectCustomerSeller(Integer.valueOf(customer.get("user_id").toString()));
				if(seller!=null){
					customer.put("sellerId", seller);
				}else{
					if(type==1){
						String b = customer.get("register_state1").toString();
						if("未注册".equals(b)){
							//根据消费者id和所扫导购所在商户id查询导购id
							Integer shopId = weiXinMapper.selectShopBySellerId(userId);
							Integer filiale = weiXinMapper.selectFilialeIdByNode2(shopId);
							Map<String,Object> m=new HashMap<>();
							m.put("shopId", shopId);
							m.put("customerId", customer.get("user_id"));
							m.put("sellerId", userId);
							m.put("filiale", filiale);
							weiXinMapper.addCustomerSeller(m);
							customer.put("sellerId", userId);
							Map<String,Object> map2=new HashMap<>();
							map2.put("sellerId", userId);
							map2.put("customerId", customer.get("user_id"));
							weiXinMapper.updateCustomer(map2);
						}else{
							customer.put("sellerId", "");
						}
					}else{
						customer.put("sellerId", "");
					}
				}
			}else{
				customer=new HashMap<>();
				WeixinReturnValue userInfo = WeiXinUtil.decodeUserInfo(map.get("encryptedData").toString(),
						map.get("iv").toString(), weixinReturnValue.getSession_key());
				if(userInfo!=null){
					if(type==1){
						customer.put("nick_name", filterEmoji(userInfo.getNickName()));
						customer.put("head_url", userInfo.getAvatarUrl());
						customer.put("gender", userInfo.getGender());
						customer.put("openid", weixinReturnValue.getOpenid());
						customer.put("unionid", weixinReturnValue.getUnionId());
						customer.put("property", 2);
						customer.put("sellerId", userId);
						customer.put("source",0);
						weiXinMapper.addCustomer(customer);
						Integer shopId = weiXinMapper.selectShopBySellerId(userId);
						Integer filiale = weiXinMapper.selectFilialeIdByNode2(shopId);
						Map<String, Object> customerSeller = new HashMap<>();
						customerSeller.put("customerId", customer.get("user_id"));
						customerSeller.put("sellerId", userId);
						customerSeller.put("shopId", shopId);
						customerSeller.put("filiale", filiale);
						weiXinMapper.addCustomerSeller(customerSeller);
						customer.put("user_id", customer.get("user_id"));
						customer.put("register_state", 0);
						customer.put("sellerId", userId);
					}else if(type==2){
						customer.put("nick_name", filterEmoji(userInfo.getNickName()));
						customer.put("head_url", userInfo.getAvatarUrl());
						customer.put("gender", userInfo.getGender());
						customer.put("openid", weixinReturnValue.getOpenid());
						customer.put("unionid", weixinReturnValue.getUnionId());
						customer.put("property", 1);
						customer.put("inviter_id", userId);
						customer.put("source",0);
						weiXinMapper.addCustomer(customer);
						customer.put("sellerId", "");
						customer.put("register_state", 0);
					}else{
						customer.put("nick_name", filterEmoji(userInfo.getNickName()));
						customer.put("head_url", userInfo.getAvatarUrl());
						customer.put("gender", userInfo.getGender());
						customer.put("openid", weixinReturnValue.getOpenid());
						customer.put("unionid", weixinReturnValue.getUnionId());
						customer.put("property", 1);
						customer.put("source",0);
						weiXinMapper.addCustomer(customer);
						customer.put("register_state", 0);
						customer.put("sellerId", "");
					}
				}
			}
			return customer;
		}else{
			return null;
		}
		
		
		
		
		
		//版本2
		/*if(weixinReturnValue !=null){
			Integer type = map.get("type") == null ? 0 : Integer.valueOf(map.get("type").toString());
			Integer userId = map.get("value") == null ? 0 : Integer.valueOf(map.get("value").toString());
			//根据openID查询消费者信息
			Map<String, Object> customer = weiXinMapper.selectCustomerByOpenid(weixinReturnValue.getOpenid());
			if(customer!=null){
				map.put("customerId", customer.get("user_id"));
				//查询消费者与当前店铺有没有建立关系
				Map<String, Object> dangQianRelation = weiXinMapper.selectRelation(map);
				if(type==1){
					Integer dangQianShopSellerId = weiXinMapper.selectSeller(map);
					if(dangQianShopSellerId!=null){
						customer.put("sellerId", dangQianShopSellerId);
					}else{
						if(dangQianRelation==null){
							weiXinMapper.addCustomerSeller(map);
						}
						customer.put("sellerId","");
					}
					//根据消费者id和所扫导购所在商户id查询导购id
					Integer shopId = weiXinMapper.selectShopBySellerId(userId);
					Map<String,Object> selectSellerId=new HashMap<>();
					selectSellerId.put("customerId", customer.get("user_id"));
					selectSellerId.put("shopId", shopId);
					Integer sellerShopSellerId = weiXinMapper.selectSeller(selectSellerId);
					Map<String,Object> m=new HashMap<>();
					m.put("shopId", shopId);
					m.put("customerId", customer.get("user_id"));
					Map<String, Object> sellerShopRelation = weiXinMapper.selectRelation(m);
					if(sellerShopSellerId==null){
						if(sellerShopRelation==null){
							m.put("sellerId", userId);
							weiXinMapper.addCustomerSeller(m);
						}else{
							weiXinMapper.updateCustomerSeller(m);
						}
					}
				}else{
					Integer dangQianShopSellerId = weiXinMapper.selectSeller(map);
					if(dangQianShopSellerId!=null){
						customer.put("sellerId", dangQianShopSellerId);
					}else{
						customer.put("sellerId","");
						if(dangQianRelation==null){
							weiXinMapper.addCustomerSeller(map);
						}
					}
				}
				return customer;
			}else{
				WeixinReturnValue userInfo = WeiXinUtil.decodeUserInfo(map.get("encryptedData").toString(),
						map.get("iv").toString(), weixinReturnValue.getSession_key());
				if(type==1){
					Map<String, Object> customerInfo = new HashMap<>();
					customerInfo.put("nick_name", userInfo.getNickName());
					customerInfo.put("head_url", userInfo.getAvatarUrl());
					customerInfo.put("gender", userInfo.getGender());
					customerInfo.put("openid", weixinReturnValue.getOpenid());
					customerInfo.put("unionid", weixinReturnValue.getUnionId());
					customerInfo.put("property", 2);
					customerInfo.put("sellerId", userId);
					weiXinMapper.addCustomer(customerInfo);
					Integer shopId = weiXinMapper.selectShopBySellerId(userId);
					Map<String, Object> customerSeller = new HashMap<>();
					customerSeller.put("customerId", customerInfo.get("user_id"));
					customerSeller.put("sellerId", userId);
					customerSeller.put("shopId", shopId);
					weiXinMapper.addCustomerSeller(customerSeller);
					customerInfo.put("user_id", customerInfo.get("user_id"));
					customerInfo.put("register_state", 0);
					customerInfo.put("sellerId", userId);
					return customerInfo;
				}else if(type==2){
					Map<String, Object> customerInfo = new HashMap<>();
					customerInfo.put("nick_name", userInfo.getNickName());
					customerInfo.put("head_url", userInfo.getAvatarUrl());
					customerInfo.put("gender", userInfo.getGender());
					customerInfo.put("openid", weixinReturnValue.getOpenid());
					customerInfo.put("unionid", weixinReturnValue.getUnionId());
					customerInfo.put("property", 1);
					customerInfo.put("inviter_id", userId);
					weiXinMapper.addCustomer(customerInfo);
					List<Map<String, Object>> customerSeller = weiXinMapper.selectCustomerSeller(userId);
					if (customerSeller != null && customerSeller.size() > 0) {
						boolean b = true;
						for (Map<String, Object> seller : customerSeller) {
							Map<String, Object> m = new HashMap<>();
							m.put("customerId", customerInfo.get("user_id"));
							m.put("shopId", seller.get("shopId"));
							m.put("sellerId", seller.get("sellerId"));
							if (map.get("shopId").toString().equals(seller.get("shopId").toString())) {
								customerInfo.put("sellerId", seller.get("sellerId"));
								b = false;
							}
							weiXinMapper.addCustomerSeller(m);
						}
						if (b) {
							Map<String, Object> m = new HashMap<>();
							m.put("customerId", customerInfo.get("user_id"));
							m.put("shopId", map.get("shopId"));
							weiXinMapper.addCustomerSeller(m);
							customerInfo.put("sellerId", "");
						}
					}
					customerInfo.put("register_state", 0);
					return customerInfo;
				}else{
					Map<String, Object> customerInfo = new HashMap<>();
					customerInfo.put("nick_name", userInfo.getNickName());
					customerInfo.put("head_url", userInfo.getAvatarUrl());
					customerInfo.put("gender", userInfo.getGender());
					customerInfo.put("openid", weixinReturnValue.getOpenid());
					customerInfo.put("unionid", weixinReturnValue.getUnionId());
					customerInfo.put("property", 1);
					weiXinMapper.addCustomer(customerInfo);
					customerInfo.put("register_state", 0);
					Map<String, Object> m = new HashMap<>();
					m.put("customerId", customerInfo.get("user_id"));
					m.put("shopId", map.get("shopId"));
					weiXinMapper.addCustomerSeller(m);
					customerInfo.put("sellerId", "");
					return customerInfo;
				}
			}
		}
		return null;
		*/
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		//版本1
		/*if (weixinReturnValue != null) {
			Integer type = map.get("type") == null ? 0 : Integer.valueOf(map.get("type").toString());
			Integer userId = map.get("value") == null ? 0 : Integer.valueOf(map.get("value").toString());
			//根据openID查询消费者信息
			Map<String, Object> customer = weiXinMapper.selectCustomerByOpenid(weixinReturnValue.getOpenid());
			//如果不为空说明已经进入过小程序
			if (customer != null) {
				map.put("customerId", customer.get("user_id"));
				//根据消费者id和商户id查询导购id
				Integer sellerId = weiXinMapper.selectSeller(map);
				if (sellerId != null) {
					customer.put("sellerId", sellerId);
				} else {
					//如果导购id为空查询消费者是否手机号注册
					boolean b = Boolean.valueOf(customer.get("register_state").toString());
					if (!b) {
						//如果没注册判断是否从导购邀请注册的
						if (type != null && type == 1) {
							Integer shopId = weiXinMapper.selectShopBySellerId(userId);
							Map<String, Object> customerSeller = new HashMap<>();
							customerSeller.put("customerId", customer.get("user_id"));
							customerSeller.put("sellerId", userId);
							customerSeller.put("shopId", shopId);
							//添加消费者和导购和
							weiXinMapper.updateCustomerSeller(customerSeller);
							customer.put("sellerId", userId);
							if(customer.get("exshopper_id")==null){
								Map<String, Object> m = new HashMap<>();
								customer.put("sellerId", userId);
								weiXinMapper.updateCustomer(m);
							}
						} else if (type != null && type == 2) {
							List<Map<String, Object>> customerSeller = weiXinMapper.selectCustomerSeller(userId);
							if (customerSeller != null && customerSeller.size() > 0) {
								boolean b1 = true;
								for (Map<String, Object> seller : customerSeller) {
									Map<String, Object> m = new HashMap<>();
									m.put("customerId", customer.get("user_id"));
									m.put("shopId", seller.get("shopId"));
									m.put("sellerId", seller.get("sellerId"));
									if (map.get("shopId").toString().equals(seller.get("shopId").toString())) {
										customer.put("sellerId", seller.get("sellerId"));
										weiXinMapper.updateCustomerSeller(m);
										b1 = false;
									} else {
										weiXinMapper.addCustomerSeller(m);
									}
								}
								if (b1) {
									Map<String, Object> m = new HashMap<>();
									m.put("customerId", customer.get("user_id"));
									m.put("shopId", map.get("shopId"));
									weiXinMapper.addCustomerSeller(m);
									customer.put("sellerId", "");
								}
							}
						}else{
							Map<String, Object> relation = weiXinMapper.selectRelation(map);
							if(relation==null){
								Map<String, Object> m = new HashMap<>();
								m.put("customerId", customer.get("user_id"));
								m.put("shopId", map.get("shopId"));
								weiXinMapper.addCustomerSeller(m);
							}
						}
					}else{
						customer.put("sellerId", "");
					}
				}
				return customer;
			}
			WeixinReturnValue userInfo = WeiXinUtil.decodeUserInfo(map.get("encryptedData").toString(),
					map.get("iv").toString(), weixinReturnValue.getSession_key());
			if (type != null && type == 1 && userInfo != null) {
				Map<String, Object> customerInfo = new HashMap<>();
				customerInfo.put("nick_name", userInfo.getNickName());
				customerInfo.put("head_url", userInfo.getAvatarUrl());
				customerInfo.put("gender", userInfo.getGender());
				customerInfo.put("openid", weixinReturnValue.getOpenid());
				customerInfo.put("unionid", weixinReturnValue.getUnionId());
				customerInfo.put("property", 2);
				customerInfo.put("sellerId", userId);
				Integer customerId = weiXinMapper.addCustomer(customerInfo);
				Integer shopId = weiXinMapper.selectShopBySellerId(userId);
				Map<String, Object> customerSeller = new HashMap<>();
				customerSeller.put("customerId", customerInfo.get("user_id"));
				customerSeller.put("sellerId", userId);
				customerSeller.put("shopId", shopId);
				weiXinMapper.addCustomerSeller(customerSeller);
				customerInfo.put("user_id", customerId);
				customerInfo.put("register_state", 0);
				customerInfo.put("sellerId", userId);
				return customerInfo;
			} else if (type != null && type == 2 && userInfo != null) {
				Map<String, Object> customerInfo = new HashMap<>();
				customerInfo.put("nick_name", userInfo.getNickName());
				customerInfo.put("head_url", userInfo.getAvatarUrl());
				customerInfo.put("gender", userInfo.getGender());
				customerInfo.put("openid", weixinReturnValue.getOpenid());
				customerInfo.put("unionid", weixinReturnValue.getUnionId());
				customerInfo.put("property", 2);
				customerInfo.put("inviter_id", userId);
				weiXinMapper.addCustomer(customerInfo);
				List<Map<String, Object>> customerSeller = weiXinMapper.selectCustomerSeller(userId);
				if (customerSeller != null && customerSeller.size() > 0) {
					boolean b = true;
					for (Map<String, Object> seller : customerSeller) {
						Map<String, Object> m = new HashMap<>();
						m.put("customerId", customerInfo.get("user_id"));
						m.put("shopId", seller.get("shopId"));
						m.put("sellerId", seller.get("sellerId"));
						if (map.get("shopId").toString().equals(seller.get("shopId").toString())) {
							customerInfo.put("sellerId", seller.get("sellerId"));
							b = false;
						}
						weiXinMapper.addCustomerSeller(m);
					}
					if (b) {
						Map<String, Object> m = new HashMap<>();
						m.put("customerId", customerInfo.get("user_id"));
						m.put("shopId", map.get("shopId"));
						weiXinMapper.addCustomerSeller(m);
						customerInfo.put("sellerId", "");
					}
				}
				customerInfo.put("register_state", 0);
				return customerInfo;
			} else {
				Map<String, Object> customerInfo = new HashMap<>();
				customerInfo.put("nick_name", userInfo.getNickName());
				customerInfo.put("head_url", userInfo.getAvatarUrl());
				customerInfo.put("gender", userInfo.getGender());
				customerInfo.put("openid", weixinReturnValue.getOpenid());
				customerInfo.put("unionid", weixinReturnValue.getUnionId());
				customerInfo.put("property", 1);
				weiXinMapper.addCustomer(customerInfo);
				customerInfo.put("register_state", 0);
				Map<String, Object> m = new HashMap<>();
				m.put("customerId", customerInfo.get("user_id"));
				m.put("shopId", map.get("shopId"));
				weiXinMapper.addCustomerSeller(m);
				customerInfo.put("sellerId", "");
				return customerInfo;
			}
		}
		return null;*/
	}

	@Override
	public boolean sendMS(String phone) {
		StringBuffer vcode = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			vcode.append(random.nextInt(10));
		}
		String content = MessageFormat.format(SysConstant.V_CODE_MESSAGE, vcode.toString()); // 要发送的短信内容，特别注意：签名必须设置，网页验证码应用需要加添加【图形识别码】。

		boolean sendSMS = SendSMS.sendSMS(phone, content, SysConstant.KLJ_MESSAGE_SUF);
		if (sendSMS) {
			CacheUtil.Cache1.put("SMS" + phone, vcode.toString());
		}
		return sendSMS;
	}

	@Override
	public ResponseMessage<Map<String,Object>> updateCustomer(Map<String, Object> map) {
		ResponseMessage<Map<String,Object>> message = new ResponseMessage<>();
		try {
				Integer num = weiXinMapper.selectWXCustomerNumByPhone(map.get("phone").toString());
				if (num > 0) {
					message.setCode("1");
					message.setMessage("手机号已经绑定其他微信！");
					return message;
				}
				Map<String,Object> customer2 = weiXinMapper.selectCustomerByPhone(map.get("phone").toString());
				Map<String, Object> customer1 = weiXinMapper.selectCustomerById(map.get("customerId")==null || "".equals(map.get("customerId"))?0:Integer.valueOf(map.get("customerId").toString()));
				if(customer2!=null){
					Map<String,Object> map1=new HashMap<>();
					map1.put("customerId", customer2.get("id"));
					map1.put("head_url", customer1.get("head_url"));
					map1.put("gender", customer1.get("gender"));
					map1.put("openid", customer1.get("openid"));
					map1.put("unionid", customer1.get("unionid"));
					map1.put("nick_name", customer1.get("nick_name"));
					map1.put("address", map.get("address"));
					map1.put("regionCode", map.get("regionCode"));
					weiXinMapper.deleteCustomerById(Integer.valueOf(map.get("customerId").toString()));
					weiXinMapper.updateCustomer(map1);
					Map<String,Object> map2=new HashMap<>();
					map2.put("customerId", customer2.get("id"));
					Integer seller = weiXinMapper.selectCustomerSeller(Integer.valueOf(customer2.get("id").toString()));
					map2.put("sellerId", seller);
					message.setResult(map2);
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
					return message;
				}else{
					if(customer1!=null){
						weiXinMapper.updateCustomer(map);
						int i=customer1.get("exshopper_id")==null || "".equals(customer1.get("exshopper_id"))?0:Integer.valueOf(customer1.get("exshopper_id").toString());
						if(i!=0){
							Integer customerCount = weiXinMapper.selectSellerCustomerCount(i);
							SellerPointEntity spe=new SellerPointEntity();
							if(customerCount>0){
								spe.setPoint(5);
							}else{
								spe.setPoint(10);
							}
							spe.setSeller_id(Long.valueOf(i));
							spe.setTitle("客户积分");
							spe.setType_index(3);
							spe.setType_name("客户");
							sellerPointEntityMapper.insertSelective(spe);
						}
						Integer relation = weiXinMapper.selectRelation(Integer.valueOf(map.get("customerId").toString()));
						if(relation==0){
							String s=map.get("regionCode")+"";
							String[] split = s.split(",");
							if(split!=null && split.length==3){
								List<String> filialeId = weiXinMapper.selectFilialeIdByAddress(split[1]);
								if(filialeId!=null && filialeId.size()>0){
									Map<String, Object> customerSeller = new HashMap<>();
									customerSeller.put("customerId", map.get("customerId"));
									customerSeller.put("filiale", filialeId.get(0));
									weiXinMapper.addCustomerSeller(customerSeller);
								}else{
									List<Integer> code = appNewActivityMapper.selectShopByCode(split[1]);
									if(code!=null && code.size()>0){
										Integer node2 = appNewActivityMapper.selectNode2ByNode1(code.get(0));
										Map<String, Object> customerSeller = new HashMap<>();
										customerSeller.put("customerId", map.get("customerId"));
										customerSeller.put("filiale", node2);
										weiXinMapper.addCustomerSeller(customerSeller);
									}
								}
							}
						}
					}
					Map<String,Object> map1=new HashMap<>();
					map1.put("customerId", map.get("customerId"));
					Integer seller = weiXinMapper.selectCustomerSeller(Integer.valueOf(map.get("customerId").toString()));
					map1.put("sellerId", seller);
					message.setResult(map1);
					message.setCode(SysConstant.SUCCESS_CODE);
					message.setMessage(SysConstant.SUCCESS_MSG);
					return message;
				}
				
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}

	@Override
	public String makeQRCode(QRCodeModel qrCodeModel) {
		String app_id = WeiXinConst.APPID;
		String app_secret = WeiXinConst.APPSECRET;
		String scene = null;
		// TODO
		if (qrCodeModel != null) {
			if (qrCodeModel.getType() != null && qrCodeModel.getValue() != null) {
				scene = "type=" + qrCodeModel.getType() + "&value=" + qrCodeModel.getValue();
			}
			if (qrCodeModel.getType() != null && qrCodeModel.getUser_id() != null) {
				scene = "type=" + qrCodeModel.getType() + "&user_id=" + qrCodeModel.getUser_id();
			}
			if (qrCodeModel.getValue() != null && qrCodeModel.getUser_id() != null) {
				scene = "value=" + qrCodeModel.getValue() + "&user_id=" + qrCodeModel.getUser_id();
			}
			if (qrCodeModel.getType() != null && qrCodeModel.getValue() != null && qrCodeModel.getUser_id() != null) {
				scene = "type=" + qrCodeModel.getType() + "&value=" + qrCodeModel.getValue() + "&user_id="
						+ qrCodeModel.getUser_id();
			}
			// if(qrCodeModel.getType() != null){
			// scene = "type="+qrCodeModel.getType();
			// }
			// if(qrCodeModel.getValue() != null){
			// scene = "value="+qrCodeModel.getValue();
			// }
			// if(qrCodeModel.getUser_id() != null){
			// scene = "user_id="+qrCodeModel.getUser_id();
			// }
		}
		try {
			String qrCode = WeiXinUtil.makeQRCode(null, null, scene, null, app_id, app_secret,
					qrCodeModel.getIshyaline());
			return qrCode;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String creadeQRcOde(Map<String, Object> map) throws Exception {
		String text = "";
		String imgurl = "";
		text = "{\"activityId\":" + map.get("activityId") + ",\"customerId\":" + map.get("customerId")+"}";
		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String path = PropUtis.getValue(PropUtis.FILE_PATH) + "/QW/" + f.format(new Date());
		System.out.println("path--------------------" + path);
		File filepath = new File(path);
		if (!filepath.exists()) {
			filepath.mkdirs();
		}
		Long time = System.currentTimeMillis();
		imgurl = PropUtis.getValue(PropUtis.FILE_URL) + "/QW/" + f.format(new Date()) + "/" + time + ".jpg";
		QRCodeUtil.encode(text, path, true,time);

		return imgurl;
	}
	
	@Override
	public List<RegionEntity> getProvince() {
		// TODO Auto-generated method stub
		return weiXinMapper.select();
	}
	
	
	@Override
	public List<RegionEntity> getCity(String code) {
		// TODO Auto-generated method stub
		return weiXinMapper.selectCity(code);
	}

	@Override
	public List<RegionEntity> getCounty(String code) {
		// TODO Auto-generated method stub
		return weiXinMapper.selectCounty(code);
	}

	@Override
	public WeiXinPhone getPhoneNumber(Map<String, Object> map) {
		WeixinReturnValue weixinReturnValue = WeiXinUtil.getSessionKey(WeiXinConst.APPID, WeiXinConst.APPSECRET,map.get("js_code").toString());
		if(weixinReturnValue!=null){
			WeiXinPhone userPhone = WeiXinUtil.decodeUserPhone(map.get("encryptedData").toString(),map.get("iv").toString(), weixinReturnValue.getSession_key());
//			System.out.println(userPhone);
			return userPhone;
		}
		return null;
	}

	@Override
	public ResponseMessage<String> touTiaoRegister(TouTiaoRegister touTiaoRegister) {
		ResponseMessage<String> message = new ResponseMessage<>();
		Integer num = weiXinMapper.selectCustomerNumByPhone(touTiaoRegister.getPhone());
		if(num>0){
			message.setCode("2");
			message.setMessage("该手机号已经注册过圣象省心宝典小程序，请进入小程序报名。");
			return message;
		}
		Map<String,Object> customer=new HashMap<>();
		customer.put("phone", touTiaoRegister.getPhone());
		customer.put("name",touTiaoRegister.getName());
		customer.put("source",touTiaoRegister.getSource());
		customer.put("province",touTiaoRegister.getProvince());
		customer.put("city",touTiaoRegister.getCity());
		customer.put("property", 1);
		customer.put("activityId", touTiaoRegister.getActivityId());
		weiXinMapper.addCustomer(customer);
		Map<String, Object> customerSeller = new HashMap<>();
		customerSeller.put("customerId", customer.get("user_id"));
		customerSeller.put("filiale", touTiaoRegister.getFilialeId());
		weiXinMapper.addCustomerSeller(customerSeller);
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		return message;
	}
	private boolean isNotEmojiCharacter(char codePoint)
	{
		return (codePoint == 0x0) ||
			(codePoint == 0x9) ||
			(codePoint == 0xA) ||
			(codePoint == 0xD) ||
			((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
			((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
			((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
	}

	/**
	 * 过滤emoji 或者 其他非文字类型的字符
	 * @param source
	 * @return
	 */
	public String filterEmoji(String source)
	{
		int len = source.length();
		StringBuilder buf = new StringBuilder(len);
		for (int i = 0; i < len; i++)
		{
			char codePoint = source.charAt(i);
			if (isNotEmojiCharacter(codePoint))
			{
				buf.append(codePoint);
			}
		}
		return buf.toString();
	}

}
